package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "Sku数据接口")
@RestController
@RequestMapping("admin/product")
public class SkuMapperController {

    @Autowired
    private ManageService manageService;


//    http://api.gmall.com/admin/product/spuSaleAttrList/17
//    回显销售属性 根据SpuID 获取销售属性，销售属性值
//    select * from spu_sale_attr ssa
//    inner join spu_sale_attr_value ssav
//    on ssa.spu_id=ssav.spu_id and ssa.base_sale_attr_id = ssav.base_sale_attr_id
//    where ssa.spu_id=17
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId){
        //调用服务层方法
           List<SpuSaleAttr>spuSaleAttrList= manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    //  http://api.gmall.com/admin/product/saveSkuInfo
    //大保存
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        //  调用服务层方法
        manageService.saveSkuInfo(skuInfo);

        //  返回数据
        return Result.ok();
    }

//    http://api.gmall.com/admin/product/list/1/10
    //分页带查询
    @GetMapping("list/{page}/{limit}")
    public Result getSkuInfoList(@PathVariable Long page,
                                 @PathVariable Long limit){
        Page<SkuInfo> skuInfoPage = new Page<>(page, limit);
        IPage skuInfoList =  manageService.getSkuInfoList(skuInfoPage);
        return Result.ok(skuInfoList);
    }
    //  商品上架
    //  http://api.gmall.com/admin/product/onSale/{skuId}
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        //  调用服务层
        manageService.onSale(skuId);
        return Result.ok();
    }

    //  商品下架
    //  http://api.gmall.com/admin/product/cancelSale/{skuId}
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        //  调用服务层
        manageService.cancelSale(skuId);
        return Result.ok();
    }

}
